package com.aidong.food.action;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.activity.biz.MstCampaignBiz;
import com.aidong.food.biz.MstFoodBiz;
import com.aidong.food.entity.MstFood;
import com.aidong.myprofile.biz.MyAddressBiz;
import com.aidong.stadium.biz.MstBrandBiz;
import com.aidong.stadium.biz.MstStoreBiz;
import com.aidong.util.CalendarUtil;
import com.aidong.util.CountDistance;
import com.aidong.util.PageUtil;
import com.aidong.util.ReadPropertiesUtil;
import com.aidong.util.SortUtil;
import com.aidong.util.bus.CodeConsts;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class MstFoodAction implements ModelDriven<MstFood> {
	private MstFood mstFood;
	private int campaignId;
	private MstBrandBiz mstBrandBiz;

	@Autowired
	public void setMstBrandBiz(MstBrandBiz mstBrandBiz) {
		this.mstBrandBiz = mstBrandBiz;
	}

	@Override
	public MstFood getModel() {
		if (mstFood == null) {
			mstFood = new MstFood();
		}
		return mstFood;
	}

	private MstFoodBiz mstFoodBiz;

	@Autowired
	public void setMstFoodBiz(MstFoodBiz mstFoodBiz) {
		this.mstFoodBiz = mstFoodBiz;
	}

	private MyAddressBiz myAddressBiz;

	@Autowired
	public void setMyAddressBiz(MyAddressBiz myAddressBiz) {
		this.myAddressBiz = myAddressBiz;
	}

	private MstStoreBiz mstStoreBiz; // 场馆service

	@Autowired
	public void setMstStoreBiz(MstStoreBiz mstStoreBiz) {
		this.mstStoreBiz = mstStoreBiz;
	}

	private MstCampaignBiz mstCampaignBiz;// 活动service

	@Autowired
	public void setMstCampaignBiz(MstCampaignBiz mstCampaignBiz) {
		this.mstCampaignBiz = mstCampaignBiz;
	}

	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	public void setCampaignId(int campaignId) {
		this.campaignId = campaignId;
	}

	private int locAddress;

	public void setLocAddress(int locAddress) {
		this.locAddress = locAddress;
	}

	private int pageCurrent; // 当前页

	/**
	 * 当前页
	 * 
	 * @param pageCurrent
	 */
	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}

	@Action(value = "foodshow", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String foodShow() {
		try {
			int foodShow = mstFoodBiz.getFoodShow();
			int resultFlag = 0;
			if (foodShow == 1) {
				// 显示
				resultFlag = 2;
			} else {
				// 0 不显示
				resultFlag = 1;
			}
			map = new HashMap<String, Object>();
			map.put("code", 1);
			map.put("result", resultFlag);// 1不显示 2显示
			return "succ";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 获取热门推荐产品
	 * 
	 * @return
	 */
	@Action(value = "hotrecommend", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String hotRecommend() {
		map = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> list = mstFoodBiz.getHotRecommend();
			List<Map<String, Object>> foods = new ArrayList<Map<String, Object>>();
			if (list.size() > 0) {
				for (Map<String, Object> food : list) {
					String picUrl = food.get("bigUrl") + "";
					food.put("bigUrl", ReadPropertiesUtil.getIMGREAD(picUrl));
					foods.add(food);
				}
			}
			map.put("code", 1);
			map.put("result", foods);
			return "succ";
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}

	}

	/**
	 * 产品列表
	 * 
	 * @return
	 */
	@Action(value = "getmstfoodhome", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getMstFoodHome() {
		map = new HashMap<String, Object>();

		if (mstFood.getGoodsType() != 0) {// areaId
			try {
				Map<String, Object> allMap = new HashMap<String, Object>();
				List<Map<String, Object>> foods = new ArrayList<Map<String, Object>>();
				int goodsType = mstFood.getGoodsType();
				if (goodsType != 8) {
					// 3餐饮 6营养品 7装备
					allMap.put("foodId", 0);
					allMap.put("chName", "全部");
					if (goodsType == 3) {
						allMap.put("bigUrl", ReadPropertiesUtil.getIMGREAD("/images/brand/meal_all_image.png"));
					} else if (goodsType == 6) {
						allMap.put("bigUrl",
								ReadPropertiesUtil.getIMGREAD("/images/brand/nourishment_all_image.jpg"));
					} else if (goodsType == 7) {
						allMap.put("bigUrl", ReadPropertiesUtil.getIMGREAD("/images/brand/equip_all_image.jpg"));
					}

					foods.add(allMap);

					Map<String, Object> paraMap = new HashMap<String, Object>();
					paraMap.put("goodsType", mstFood.getGoodsType());

					List<Map<String, Object>> list = mstFoodBiz.getMstFoodHome(paraMap);
					if (list.size() > 0) {
						for (Map<String, Object> food : list) {
							String picUrl = food.get("bigUrl") + "";
							food.put("bigUrl", ReadPropertiesUtil.getIMGREAD(picUrl));
							foods.add(food);
						}
					}
				} else {
					// 赛事
					Map<String, Object> paraMap = new HashMap<String, Object>();
					paraMap.put("cityId", mstFood.getAreaId());
					List<Map<String, Object>> list = mstCampaignBiz.getMstMatch(paraMap);
					if (list.size() > 0) {
						for (Map<String, Object> food : list) {
							String picUrl = food.get("bigUrl") + "";
							food.put("bigUrl", ReadPropertiesUtil.getIMGREAD(picUrl));
							foods.add(food);
						}
					}
				}

				map.put("code", 1);
				map.put("result", foods);
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		} else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}

	}

	/**
	 * 供应商规则列表
	 * 
	 * @return
	 */
	@Action(value = "getmstsupplyrule", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getMstSupplyRule() {
		map = new HashMap<String, Object>();
		try {
			int foodId = mstFood.getFoodId();
			// int storeId = mstFood.getStoreId();
			// int code = mstFood.getCode(); // code
			int cityId = mstFood.getCityId(); // 城市ID
			int regionId = mstFood.getRegionId(); // 区域ID
			int districtId = mstFood.getDistrictId(); // 商圈ID
			int goodsType = mstFood.getGoodsType();

			Map<String, Object> pageMap = PageUtil.page(20, mstFood.getPageCurrent());
			// Map<String, Object> pageMap = new HashMap<String,Object>();
			pageMap.put("foodId", foodId);
			String brandDict = "";
			if (goodsType == 3) {
				brandDict = "foodBrandDict";
			} else if (goodsType == 6) {
				brandDict = "nourishmentBrandDict";
			} else if (goodsType == 7) {
				brandDict = "equipBrandDict";
			}
			pageMap.put("brandDict", brandDict);
			pageMap.put("cityId", cityId);
			pageMap.put("regionId", regionId);
			pageMap.put("districtId", districtId);
			pageMap.put("storeId", 0);
			pageMap.put("goodsType", goodsType);

			List<Map<String, Object>> rules = mstFoodBiz.getMstSupplyRule(pageMap);
			if (rules.size() > 0) {
				for (Map<String, Object> rule : rules) {
					String picUrl = rule.get("picUrl") + "";
					rule.put("picUrl", ReadPropertiesUtil.getIMGREAD(picUrl));
				}
			}
			map.put("code", 1);
			map.put("result", rules);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 供应商规则列表
	 * 
	 * @return
	 */
	@Action(value = "getmstsupplyrulebystore", results = {
			@Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getMstSupplyRuleByStore() {
		map = new HashMap<String, Object>();
		try {
			int storeId = mstFood.getStoreId();
			int goodsType = mstFood.getGoodsType();
			// Map<String, Object> paraMap = new HashMap<String, Object>();
			Map<String, Object> pageMap = PageUtil.page(20, mstFood.getPageCurrent());
			List<Map<String, Object>> rules = new ArrayList<Map<String, Object>>();

			if (goodsType == 8) {
				/*原始版本
				 * List<Map<String, Object>> matchs = mstCampaignBiz.getOneStoreMatchInfo(storeId);
				if (matchs.size() > 0) {
					for (Map<String, Object> match : matchs) {*/
				/**
				 * 20160614
				 * 修改
				 */
				pageMap.put("storeId", storeId);
				rules = mstCampaignBiz.getOneStoreMatchInfo(pageMap);
				if (rules.size() > 0) {
					for (Map<String, Object> match : rules) {
						//match.put("picUrl", ReadPropertiesUtil.getIMGREAD(match.get("smallUrl")));
						match.put("picUrl", ReadPropertiesUtil.get300Img(match.get("smallUrl")));
						String stratDate = match.get("stratDate").toString(); // 活动开始日期
						String stratTime = match.get("stratTime").toString(); // 活动开始时间
						String week = match.get("week").toString(); // 周
						match.put("campaignStartTime", stratDate + "(" + week + ")" + stratTime); // 活动开始时间(前端显示)
						match.put("lookMore", "http://www.baidu.com");
						match.remove("stratDate");
						match.remove("stratTime");
						match.remove("week");
						match.put("storeName", stratDate + "(" + week + ")" + stratTime);

					}
				}
			} else {
				pageMap.put("storeId", storeId);
				pageMap.put("goodsType", goodsType);
				String brandDict = "";
				if (goodsType == 3) {
					brandDict = "foodBrandDict";
				} else if (goodsType == 6) {
					brandDict = "nourishmentBrandDict";
				} else if (goodsType == 7) {
					brandDict = "equipBrandDict";
				}
				pageMap.put("brandDict", brandDict);
				pageMap.put("cityId", 0);
				pageMap.put("regionId", 0);
				pageMap.put("districtId", 0);
				pageMap.put("foodId", 0);
				rules = mstFoodBiz.getMstSupplyRule(pageMap);
				if (rules.size() > 0) {
					for (Map<String, Object> rule : rules) {
						String picUrl = rule.get("picUrl") + "";
						rule.put("picUrl", ReadPropertiesUtil.getIMGREAD(picUrl));
					}
				}
			}

			map.put("code", 1);
			map.put("result", rules);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 供应规则详情
	 * 
	 * @return
	 */
	@Action(value = "getruledetail", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getMstRuleDetail() {
		map = new HashMap<String, Object>();
		try {
			int ruleId = mstFood.getRuleId();
			int goodsType = mstFood.getGoodsType();
			//if (ruleId > 0 && goodsType > 0) {
			if (ruleId > 0) {
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("goodsType", goodsType);
				paraMap.put("ruleId", ruleId);
				Map<String, Object> ruleMap = mstFoodBiz.getMstRuleDetail(paraMap);
				if (ruleMap != null) {
					ruleMap.put("bigUrl", ReadPropertiesUtil.getIMGREAD(ruleMap.get("bigUrl")));
					ruleMap.put("takeFoodDate", ruleMap.get("formDate") + "-" + ruleMap.get("toDate"));
					ruleMap.put("takeFoodPlace", ruleMap.get("takeFoodPlace") + "");
					ruleMap.put("evidenceText", "使用凭证");
					ruleMap.put("evidence", ruleMap.get("evidence"));
					ruleMap.put("refundKnowText", "退款需知");
					ruleMap.put("refundKnow", ruleMap.get("refundKnow"));
					ruleMap.put("warmPromptText", "温馨提示：");
					ruleMap.put("warmPrompt", ruleMap.get("warmPrompt"));
					ruleMap.remove("formDate");
					ruleMap.remove("toDate");
					ruleMap.put("price", new  DecimalFormat("#0.00").format(Double.parseDouble(ruleMap.get("price").toString())));
					ruleMap.put("heat", 100);// 热度值需要进行处理。。。。

					map.put("code", 1);
					map.put("result", ruleMap);
					return "succ";
				} else {
					map.put("code", 0);
					map.put("msg", "没有此供应规则");
					return "succ";
				}
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 购买信息
	 * 
	 * @return
	 */
	@Action(value = "getpurchaseinfo", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getPurchaseInfo() {
		map = new HashMap<String, Object>();

		try {
			int ruleId = mstFood.getRuleId();// 根据规则ID获取适用月份
			int goodsType = mstFood.getGoodsType();
			int idongId = mstFood.getIdongId();
			int foodId = mstFood.getFoodId();
			int storeId = mstFood.getStoreId();// 门店ID
			int gbId	=mstFood.getGbId();//商品品牌ID
			int cityId = mstFood.getCityId();
			if (goodsType > 0) {
				if (goodsType != 8) {
					if (ruleId < 0) {
						map.put("code", 0);
						map.put("msg", "参数缺失");
						return "succ";
					}
				}
				// 购买时间安排
				List<Map<String, Object>> buyDates = CalendarUtil.getBuyDate();
				
				// List<Map<String, Object>> buyDatesResult = new
				// ArrayList<Map<String, Object>>();
				// 购买信息
				Map<String, Object> buyInfo = new HashMap<String, Object>();
				if (ruleId > 0 || foodId > 0) {
					// 购买信息
					// foodBrandDict 有问题不能写死的
					Map<String, Object> paraMap1 = new HashMap<String, Object>();
					paraMap1.put("ruleId", ruleId);
					paraMap1.put("storeId", storeId);
					String brandDict = "";
					if (goodsType == 3) {
						brandDict = "foodBrandDict";
					} else if (goodsType == 6) {
						brandDict = "nourishmentBrandDict";
					} else if (goodsType == 7) {
						brandDict = "equipBrandDict";
					}
					paraMap1.put("brandDict", brandDict);
					paraMap1.put("foodId", foodId);
					paraMap1.put("gbId", gbId);
					paraMap1.put("goodsType", goodsType);
					paraMap1.put("cityId", cityId);
					buyInfo = mstFoodBiz.getPurchaseInfo(paraMap1);
					String istoday="0";
					if (buyInfo != null) {
						istoday=buyInfo.get("isToDay").toString();
						if (goodsType == 3 && "0".equals(istoday)) {
							buyDates = CalendarUtil.getBuyDateFood();
						}
						buyInfo.put("picUrl", ReadPropertiesUtil.getIMGREAD(buyInfo.get("picUrl")));
						if (foodId > 0) {
							if (storeId > 0) {
								buyInfo.put("takeFoodPlace", buyInfo.get("storeName"));
							} else {
								buyInfo.put("takeFoodPlace", "");
								buyInfo.put("storeName", "");
								buyInfo.put("storeId", "");
							}
						} else {
							buyInfo.put("takeFoodPlace", buyInfo.get("storeName"));
						}
						buyInfo.put("freight", CodeConsts.Freight);// 运费--需要配置
						if (ruleId <= 0) {
							ruleId = Integer.parseInt(buyInfo.get("ruleId") + "");
						}
					} else {
						map.put("code", 0);
						map.put("msg", "没有此购买信息");
						return "succ";
					}

					// 不可购买日期
					List<Map<String, Object>> unBuyDates = mstFoodBiz.getFoodUnBuy(ruleId);

					for (Map<String, Object> buyDate : buyDates) {
						// 根据日期取得不同的取餐时间区间
						String apply = buyDate.get("applyDate").toString();
						Map<String, Object> paraMap = new HashMap<String, Object>();
						// paraMap.put("yearMonth", apply);
						paraMap.put("ruleId", ruleId);
						Map<String, Object> applyDate = mstFoodBiz.getMealPickUp(paraMap);
						if (applyDate != null) {
							String pickMealDate = applyDate.get("pickForm") + "-" + applyDate.get("pickTo");
							buyDate.put("pickMealDate", pickMealDate);
						} else {
							buyDate.put("pickMealDate", "");
						}
						// 日期购买状态 0.可买 1.不可买
						String inDate = buyDate.get("yearDate").toString();
						if (unBuyDates.size() > 0) {
							for (Map<String, Object> unBuyDate : unBuyDates) {
								String unDate = unBuyDate.get("unBuyDate").toString(); // 不可购买日期
								if (unDate.equals(inDate)) {
									buyDate.put("buyFlg", 1);
									break;
								} else {
									buyDate.put("buyFlg", 0);
								}
							}
						} else {
							buyDate.put("buyFlg", 0);
						}
						//增加餐饮判断
						if(goodsType == 3){
							// 当前时间是否已超过当天截止下单时间
							if("0".equals(istoday)){
								if (inDate.equals(new SimpleDateFormat("yyyy-MM-dd").format(CalendarUtil.getNextDay(new Date(),1)))) {
									// 当前时间减去50分钟
									String hour = new SimpleDateFormat("HH:mm").format(new Date().getTime());
									String formHour = new SimpleDateFormat("HH:mm").format(
											new SimpleDateFormat("HH:mm").parse(buyInfo.get("cutOffDate").toString()).getTime()
													);
									int compareResult = hour.compareTo(formHour);
									if (compareResult > 0) {
										buyDate.put("buyFlg", 1);
									}
								}
							}else{
								if (inDate.equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))) {
									// 当前时间减去50分钟
									String hour = new SimpleDateFormat("HH:mm").format(new Date().getTime());
									String formHour = new SimpleDateFormat("HH:mm").format(
											new SimpleDateFormat("HH:mm").parse(buyInfo.get("cutOffDate").toString()).getTime()
													);
									int compareResult = hour.compareTo(formHour);
									if (compareResult > 0) {
										buyDate.put("buyFlg", 1);
									}
								}
							}
							
						}else{
							// 当前时间是否已超过当天截止下单时间
							if (inDate.equals(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))) {
								// 当前时间减去50分钟
								String hour = new SimpleDateFormat("HH:mm").format(new Date().getTime());
								String formHour = new SimpleDateFormat("HH:mm").format(
										new SimpleDateFormat("HH:mm").parse(buyInfo.get("cutOffDate").toString()).getTime()
												- 60000 * 50);
								int compareResult = hour.compareTo(formHour);
								if (compareResult > 0) {
									buyDate.put("buyFlg", 1);
								}
							}
						}
						buyDate.put("selectDate", buyDate.get("week") + " " + buyDate.get("pageDate"));
						// buyDate.remove("yearDate");
						buyDate.remove("applyDate");
						buyDate.remove("week");
					}
				} else if (ruleId == 0) {// 票务
					// 获取活动的信息
					Map<String, Object> duceMap = mstCampaignBiz.getOneCampaignInfo(campaignId);
					buyInfo.put("picUrl", ReadPropertiesUtil.getIMGREAD(duceMap.get("smallUrl")));
					buyInfo.put("takeFoodPlace", "");
					buyInfo.put("price", duceMap.get("price"));
					buyInfo.put("storeName", "");
					buyInfo.put("pickUpWay", duceMap.get("pickUpWay"));
					buyInfo.put("brandName", "");
					buyInfo.put("foodName", duceMap.get("campaignTitle"));
					buyInfo.put("brandName", duceMap.get("brandName"));
					buyInfo.put("freight", CodeConsts.Freight);// 运费--需要配置

					for (Map<String, Object> buyDate : buyDates) {
						// 根据日期取得不同的取餐时间区间
						buyDate.put("pickMealDate", "09:00-20:00");
						buyDate.put("buyFlg", 0);
						buyDate.put("selectDate", buyDate.get("week") + " " + buyDate.get("pageDate"));
						// buyDate.remove("yearDate");
						buyDate.remove("applyDate");
						buyDate.remove("week");
					}

				}
				buyInfo.remove("formDate");
				buyInfo.put("buyDates", buyDates);

				// 获取默认地址
				List<Map<String, Object>> addressInfo = myAddressBiz.getReceiverAddressDefault(idongId);
				if (addressInfo.size() > 0) {
					buyInfo.put("addressDefault", addressInfo.get(0));
				} else {
					buyInfo.put("addressDefault", new JSONObject());
				}

				// 行政区域列表
				List<Map<String, Object>> regions = new ArrayList<Map<String, Object>>();
				if (goodsType == 8) { // 票务-需要选门店
					if (campaignId > 0) {
						// 城市列表
						List<Map<String, Object>> citys = mstStoreBiz.getMstCity("");
						// 行政区域结果列表
						List<Map<String, Object>> regionsResult = new ArrayList<Map<String, Object>>();

						try {
							for (Map<String, Object> city : citys) {
								// 区列表
								List<Map<String, Object>> regionList = mstStoreBiz
										.getMstRegion(city.get("areaId").toString());
								// 查询该门店可取票区
								List<Map<String, Object>> districtsCamp = mstCampaignBiz.getEventAreas(campaignId);

								for (Map<String, Object> region : regionList) {
									int flag = 0;
									for (int jj = 0; jj < districtsCamp.size(); jj++) {
										if (region.get("areaId").equals(districtsCamp.get(jj).get("areaId"))) {
											flag = 1;
											break;
										}
									}
									if (flag == 1) {
										regionsResult.add(region);
									}
								}
								for (Map<String, Object> region : regionsResult) {
									List<Map<String, Object>> districts = new ArrayList<Map<String, Object>>();

									// 获取可取票 某区的商圈列表
									Map<String, Object> paraMap = new HashMap<String, Object>();
									paraMap.put("areaId", region.get("areaId").toString());
									paraMap.put("campaignId", campaignId);
									List<Map<String, Object>> storesMap = mstCampaignBiz.getEventAreaStore(paraMap);
									for (int k = 0; k < storesMap.size(); k++) {
										if (region.get("areaId").toString().equals(storesMap.get(k).get("areaId"))) {
											districts.add(storesMap.get(k));
										}
									}
									region.put("district", districts);
								}
								for (int i = 0; i < regionsResult.size(); i++) {
									regions.add(regionsResult.get(i));
								}
							}
							buyInfo.put("regions", regions);
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
					} else {
						map.put("code", 0);
						map.put("msg", "参数缺失");
						return "succ";
					}
				}
				buyInfo.put("maxNum", 999);// --需要配置
				buyInfo.put("regions", regions);

				map.put("code", 1);
				map.put("result", buyInfo);
				return "succ";
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 产品筛选内容
	 * 
	 * @return
	 */
	@Action(value = "getfoodsifting", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getFoodSifting() {
		map = new HashMap<String, Object>();
		try {
			// 城市列表
			List<Map<String, Object>> citys = mstStoreBiz.getMstCity(mstFood.getAreaId());
			// 行政区域列表
			List<Map<String, Object>> regions = new ArrayList<Map<String, Object>>();

			// 热门商圈列表
			Map<String, Object> areas = new HashMap<String, Object>();
			areas.put("areaId", "0");
			areas.put("areaName", "热门商圈");

			// 热门商圈中的热门商圈
			List<Map<String, Object>> dishotMap = new ArrayList<Map<String, Object>>();

			// 热门商圈中的全部商圈
			Map<String, Object> total = new HashMap<String, Object>();
			total.put("areaId", "0");
			total.put("areaName", "全部商圈");
			dishotMap.add(total);

			List<Map<String, Object>> hots = mstStoreBiz.getMstHotDistrict(mstFood.getAreaId());
			for (int i = 0; i < hots.size(); i++) {
				dishotMap.add(hots.get(i));
			}

			areas.put("district", dishotMap);

			regions.add(areas);

			for (Map<String, Object> city : citys) {
				// 区列表
				List<Map<String, Object>> regionList = mstStoreBiz.getMstRegion(city.get("areaId").toString());
				for (Map<String, Object> region : regionList) {
					// 商圈列表
					List<Map<String, Object>> districts = new ArrayList<Map<String, Object>>();

					Map<String, Object> allDis = new HashMap<String, Object>();
					allDis.put("areaId", "0");
					allDis.put("areaName", "全部商圈");
					districts.add(allDis);

					List<Map<String, Object>> listMap = mstStoreBiz.getMstDistrict(region.get("areaId").toString());
					for (int i = 0; i < listMap.size(); i++) {
						districts.add(listMap.get(i));
					}

					region.put("district", districts);
				}
				for (int i = 0; i < regionList.size(); i++) {
					regions.add(regionList.get(i));
				}
			}

			// 产品
			List<Map<String, Object>> foods = new ArrayList<Map<String, Object>>();
			Map<String, Object> allFoodMap = new HashMap<String, Object>();
			allFoodMap.put("foodId", "0");
			allFoodMap.put("chName", "全部");
			foods.add(allFoodMap);

			Map<String, Object> resultMap = new HashMap<String, Object>();

			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("goodsType", mstFood.getGoodsType());
			List<Map<String, Object>> foodMap = mstFoodBiz.getMstFoodHome(paraMap);

			if (foodMap.size() == 0) {
				resultMap.put("foods", foods);
			} else {
				for (Map<String, Object> food : foodMap) {
					foods.add(food);
					resultMap.put("foods", foods);
					// 门店
					/*
					 * List<Map<String, Object>> stores = new
					 * ArrayList<Map<String, Object>>(); Map<String, Object>
					 * allStoreMap = new HashMap<String, Object>();
					 * allStoreMap.put("storeId", 0);
					 * allStoreMap.put("storeName", "全部场馆");
					 * stores.add(allStoreMap);
					 * 
					 * List<Map<String, Object>> storeMap =
					 * mstFoodBiz.getSupplyStore(); for (Map<String, Object>
					 * store : storeMap) { stores.add(store); }
					 */

				}
			}
			map.put("code", 1);
			resultMap.put("regions", regions);
			map.put("result", resultMap);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 产品筛选内容最新
	 * 
	 * @return
	 */
	@Action(value = "getfoodsiftingnew", results = {
			@Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getfoodsiftingnew() {
		map = new HashMap<String, Object>();
		int goodsType=mstFood.getGoodsType();
		try {
			// 获取商品分类
			List<Map<String, Object>> types = mstFoodBiz.getType("goodsTypeDict", 1);
			List<Map<String, Object>> regions = new ArrayList<Map<String, Object>>();
			Map<String, Object> re = new HashMap<String, Object>();
			re.put("dictCode", "");
			re.put("dictName", "全部分类");
			re.put("dictProp", "");
			List<Object> list = new ArrayList<Object>();
			list.add(re);
			Map<String, Object> areas = new HashMap<String, Object>();
			areas.put("dictCode", "");
			areas.put("dictName", "全部分类");
			areas.put("list", list);
			regions.add(areas);
			// 获取详情列表
			for (Map<String, Object> type : types) {
				type.put("list", null);
				if ("3".equals(type.get("dictCode"))) {
					List<Map<String, Object>> detailTypes = mstFoodBiz.getType("healthFoodType" + "", 1);
					detailTypes.add(0, re);
					type.put("list", detailTypes);
				} else if ("6".equals(type.get("dictCode"))) {
					List<Map<String, Object>> detailTypes = mstFoodBiz.getType("nourishmentType" + "", 1);
					detailTypes.add(0, re);
					type.put("list", detailTypes);
				} else if ("7".equals(type.get("dictCode"))) {
					List<Map<String, Object>> detailTypes = mstFoodBiz.getType("equipType" + "", 1);
					detailTypes.add(0, re);
					type.put("list", detailTypes);
				}
				regions.add(type);
			}
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("regions", regions);
			Map<String,Object> ms=new HashMap<String,Object>();
			if(goodsType==3){
				ms.put("dictType", 31);
			}
			if(goodsType==6){
				ms.put("dictType", 39);
			}
			if(goodsType==7){
				ms.put("dictType", 40);
			}
			List<Map<String, Object>> brandMap = mstFoodBiz.getBrandType(ms);
			Map<String, Object> area = new HashMap<String, Object>();
			area.put("dictCode", "");
			area.put("dictName", "全部品牌");
			area.put("dictProp", "");
			brandMap.add(0, area);
			resultMap.put("brands", brandMap);

			map.put("code", 1);
			map.put("result", resultMap);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 产品筛选内容最新
	 * 
	 * @return
	 */
	@Action(value = "getfoodByGoodsType", results = {
			@Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getfoodByGoodsType() {
		map = new HashMap<String, Object>();
		
		int goodsType=mstFood.getGoodsType();
		try {
			// 获取商品分类
			List<Map<String, Object>> types = mstFoodBiz.getType("goodsTypeDict", 1);
			types.remove(0);
			List<Map<String, Object>> regions = new ArrayList<Map<String, Object>>();
			Map<String, Object> re = new HashMap<String, Object>();
			re.put("dictCode", "");
			re.put("dictName", "全部分类");
			re.put("dictProp", "");
			List<Object> list = new ArrayList<Object>();
			list.add(re);
			Map<String, Object> areas = new HashMap<String, Object>();
			areas.put("dictCode", "");
			areas.put("dictName", "全部分类");
			areas.put("list", list);
			regions.add(areas);
			// 获取详情列表
			for (Map<String, Object> type : types) {
				type.put("list", null);
				/*if ("3".equals(type.get("dictCode"))) {
					List<Map<String, Object>> detailTypes = mstFoodBiz.getType("healthFoodType" + "", 1);
					detailTypes.add(0, re);
					type.put("list", detailTypes);
				} else */if ("6".equals(type.get("dictCode"))) {
					List<Map<String, Object>> detailTypes = mstFoodBiz.getType("nourishmentType" + "", 1);
					detailTypes.add(0, re);
					type.put("list", detailTypes);
				} else if ("7".equals(type.get("dictCode"))) {
					List<Map<String, Object>> detailTypes = mstFoodBiz.getType("equipType" + "", 1);
					detailTypes.add(0, re);
					type.put("list", detailTypes);
				}
				regions.add(type);
			}
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("regions", regions);
			Map<String,Object> ms=new HashMap<String,Object>();
			/*if(goodsType==3){
				ms.put("dictType", 31);
			}*/
			if(goodsType==6){
				ms.put("dictType", 39);
			}
			if(goodsType==7){
				ms.put("dictType", 40);
			}
			List<Map<String, Object>> brandMap = mstFoodBiz.getBrandTypeN(ms);
			Map<String, Object> area = new HashMap<String, Object>();
			area.put("dictCode", "");
			area.put("dictName", "全部品牌");
			area.put("dictProp", "");
			brandMap.add(0, area);
			resultMap.put("brands", brandMap);

			map.put("code", 1);
			map.put("result", resultMap);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	@Action(value = "getmstsupplyruleNew", results = {
			@Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getmstsupplyruleNew() {
		map = new HashMap<String, Object>();
		try {
			int brandId = mstFood.getBrandId();// 品牌ID
			int pBrandId = mstFood.getPbId();// 父品牌ID；
			int typeId = mstFood.getTypeId();// 分类ID
			int pTypeId = mstFood.getPid();// 父分类ID
			int cityId = mstFood.getCityId();//城市ID
			Map<String, Object> resultMap = new HashMap<String, Object>();

			Map<String, Object> pageMap = PageUtil.page(10, mstFood.getPageCurrent());
			String brandDict = "";
			if (pTypeId == 3 || pBrandId == 3) {
				brandDict = "foodBrandDict";
			} else if (pTypeId == 6 || pBrandId == 6) {
				brandDict = "nourishmentBrandDict";
			} else if (pTypeId == 7 || pBrandId == 7) {
				brandDict = "equipBrandDict";
			}
			pageMap.put("brandDict", brandDict);
			pageMap.put("pBrandId", pBrandId);
			pageMap.put("brandId", brandId);
			pageMap.put("typeId", typeId);
			pageMap.put("pTypeId", pTypeId);
			pageMap.put("cityId", cityId);
			List<Map<String, Object>> rules = new ArrayList<Map<String, Object>>();
			if (pTypeId > 0 || pBrandId > 0) {
				rules = mstFoodBiz.getMstSupplyRuleNew(pageMap);
			} else {
				rules = mstFoodBiz.getMstSupplyRuleNew2(pageMap);
			}
			if (rules.size() > 0) {
				for (Map<String, Object> rule : rules) {
					String picUrl = rule.get("gpPic") + "";
					//rule.put("gpPic", ReadPropertiesUtil.getIMGREAD(picUrl));
					rule.put("gpPic", ReadPropertiesUtil.get300Img(picUrl));
				}
			}
			resultMap.put("ListValue", rules);
			map.put("code", 1);
			map.put("result", resultMap);
			map.put("pageCurrent", pageMap.get("pageCurrent")); // 当前页
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	@Action(value = "getruledetailNew", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getruledetailNew() {
		map = new HashMap<String, Object>();
		try {
			int foodId = mstFood.getFoodId();
			int goodsType = mstFood.getGoodsType();
			int storeId = mstFood.getStoreId();
			
			String price = mstFood.getPrice();
			int brandId=mstFood.getBrandId();//品牌ID
			int idongId=mstFood.getIdongId();//爱动号
			if (foodId > 0 && goodsType > 0) {
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("goodsType", goodsType);
				paraMap.put("foodId", foodId);
				paraMap.put("storeId", storeId);
				paraMap.put("brandId", brandId);
				paraMap.put("idongId", idongId);
				Map<String, Object> ruleMap = null;
				
				/*if (storeId > 0) {
					paraMap.put("storeId", storeId);
					ruleMap = mstFoodBiz.getMstRuleDetailNew(paraMap);
				} else {*/
					if(idongId>0 && storeId==0){
						paraMap.put("cityId", mstFood.getCityId());
						Map<String, Object> re=mstFoodBiz.getStoreByFoodIdAndID(paraMap);
						if(re!=null && re.containsKey("storeId")){
							storeId=Integer.parseInt(re.get("storeId").toString());
						}
					}
					//-----如果沒有storeId，取價格最低的商鋪ID-------//
					if(storeId==0){
						Map<String,Object> smp=new HashMap<String,Object>();
						smp.put("foodId", foodId);
						smp.put("cityId", mstFood.getCityId());
						storeId = mstFoodBiz.getDefaultStoreId(smp);
					}
					if(storeId>0){
						paraMap.put("storeId", storeId);
						ruleMap = mstFoodBiz.getMstRuleDetailNew(paraMap);
					}else{
						ruleMap = mstFoodBiz.getFoodDetailById(paraMap);
						ruleMap.put("brandId", brandId);
					}
				//}
				if (ruleMap != null) {
					ruleMap.put("bigUrl", ReadPropertiesUtil.getIMGREAD(ruleMap.get("bigUrl")));
					ruleMap.put("takeFoodDate", ruleMap.get("formDate") + "-" + ruleMap.get("toDate"));
					ruleMap.put("takeFoodPlace", ruleMap.get("takeFoodPlace"));
					ruleMap.put("evidenceText", "使用凭证");
					ruleMap.put("refundKnowText", "退款需知");
					ruleMap.put("warmPromptText", "温馨提示：");
					ruleMap.put("price", new  DecimalFormat("#0.00").format(Double.parseDouble(ruleMap.get("price").toString())));
					if (storeId > 0) {
						ruleMap.put("evidence", ruleMap.get("evidence"));
						ruleMap.put("refundKnow", ruleMap.get("refundKnow"));
						ruleMap.put("warmPrompt", ruleMap.get("warmPrompt"));
					} else {
						ruleMap.put("warmPrompt", "请于门店营业结束前取货");
						ruleMap.put("refundKnow", "支持随时退款；超过取货时间48小时如未取货将自动退款。（此规则只适用于营养品和装备）");
						ruleMap.put("evidence", "于取货门店出示条形码，扫码核销后即可取货");
						ruleMap.put("price", price);
					}
					ruleMap.remove("formDate");
					ruleMap.remove("toDate");
					ruleMap.put("heat", 100);// 热度值需要进行处理。。。。

					map.put("code", 1);
					map.put("result", ruleMap);
					return "succ";
				} else {
					map.put("code", 0);
					map.put("msg", "没有此供应规则");
					return "succ";
				}
			} else {
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 自提地址区域和品牌
	 * 
	 * @return
	 */
	@Action(value = "getAddress", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getAddress() {
		map = new HashMap<String, Object>();
		try {
			// 城市列表
			int foodId = mstFood.getFoodId();
			List<Map<String, Object>> citys = mstStoreBiz.getMstCity(mstFood.getCityId()+"");
			// 行政区域列表
			List<Map<String, Object>> regions = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> location = new ArrayList<Map<String, Object>>();
			for (Map<String, Object> city : citys) {
				Map<String,Object> ma=new HashMap<String,Object>();
				//ma.put("foodId", foodId);
				ma.put("pAreaId",city.get("areaId").toString() );
				// 区列表
				List<Map<String, Object>> regionList = mstStoreBiz.getMstRegion(city.get("areaId").toString());

				for (int i = 0; i < regionList.size(); i++) {
					regions.add(regionList.get(i));
				}
			}
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("areaId", "1001");
			map1.put("areaName", "常用自提地址");
			location.add(map1);
			// 产品
			List<Map<String, Object>> brandMap = mstBrandBiz.getMstBrandMoreNew(foodId+"");
			Map<String, Object> allFoodMap = new HashMap<String, Object>();
			allFoodMap.put("areaId", "");
			allFoodMap.put("areaName", "全部区域");
			regions.add(0, allFoodMap);
			allFoodMap = new HashMap<String, Object>();
			allFoodMap.put("brandId", "");
			allFoodMap.put("chName", "全部品牌");
			brandMap.add(0, allFoodMap);

			Map<String, Object> resultMap = new HashMap<String, Object>();
			map.put("code", 1);
			resultMap.put("regions", regions);
			resultMap.put("brand", brandMap);

			resultMap.put("location", location);
			map.put("result", resultMap);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 自提地址列表
	 * 
	 * @return
	 */
	@Action(value = "getAddressList", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getAddressList() {
		map = new HashMap<String, Object>();
		if(mstFood.getPageCurrent()>1){
		    Map<String,Object> result = new HashMap<String,Object>();
		    result.put("store", new ArrayList<>());
			map.put("code", 1);
			map.put("pageCurrent", mstFood.getPageCurrent()); // 当前页
			map.put("result",result);
			return "succ";
		}
		
		try {
			String areaId = mstFood.getAreaId();// 区域ID
			int cityId = mstFood.getCityId();// 城市ID
			int brandId = mstFood.getBrandId();// 品牌ID
			int foodId = mstFood.getFoodId();// 商品ID
			int idongId=mstFood.getIdongId();//爱动号
			int goodsType=mstFood.getGoodsType();//商品类型ID
			int gbId=mstFood.getGbId();//商品品牌ID
		
			Map<String, Object> pageMap = PageUtil.page(2000, mstFood.getPageCurrent());
			pageMap.put("cityId", cityId);
			pageMap.put("areaId", areaId);
			pageMap.put("brandId", brandId);
			pageMap.put("foodId", foodId);
			pageMap.put("idongId", idongId);
			pageMap.put("goodsType", goodsType);
			pageMap.put("gbid", gbId);
			// 门店列表
			List<Map<String, Object>> storeMap = new ArrayList<Map<String, Object>>();
			if (locAddress > 0) {
				if(idongId > 0){
					storeMap = mstFoodBiz.getMyStoreByFoodId(pageMap);
				}
			}else{
				storeMap = mstFoodBiz.getMstStoreByFoodId(pageMap);
			}
			if (storeMap.size() > 0) {
				for (Map<String, Object> store : storeMap) {
					String coordinate = store.get("coordinate") + ""; // 场馆所在经纬度
					double lon = mstFood.getLongitude();
					double lat = mstFood.getLatitude();
					if (!"".equals(coordinate) && coordinate != null && lon > 0 && lat > 0) {
						String[] location = coordinate.split(",");
						double discance = CountDistance.distance(lon, lat, Double.parseDouble(location[0]),
								Double.parseDouble(location[1]));
						int dis = (int) Math.rint(discance); // 四舍五入取整数
						int distance = dis / 1000;
						store.put("count", dis);
						if (distance < 1) { // 相距距离 小于一公里显示米 大于一公里显示千米
							if (dis <= 100) {
								store.put("distance", "<100m");
							} else {
								store.put("distance", dis + " m");
							}
						} else {
							store.put("distance", distance + " km");
						}
					} else {
						store.put("count", 0);
					}
					//store.remove("coordinate");
				}

				// 按距离排序
				SortUtil.distanceSort(storeMap);
			}
			Map<String, Object> resultMap = new HashMap<String, Object>();
			map.put("code", 1);
			resultMap.put("store", storeMap);
			map.put("pageCurrent", pageMap.get("pageCurrent")); // 当前页
			map.put("result", resultMap);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}
